package com.aaa.web.controller.weblogin;

import com.aaa.entity.Employee;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.Result;
import com.aaa.web.service.iwebloginservice.IWebLoginService;
import com.aaa.web.systemconst.ISystemConst;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/** 
* @program:
* @description:
* @author:
* @create:
**/
@Controller
@RequestMapping("/weblogin")
public class WebLoginController {

    @Autowired
    private IWebLoginService iWebLoginService;

    @RequestMapping("/goLogin")
    public String goLogin(){
        EmpUtil.setEmp(null);
        return "views/webhtml/login/login";
    }

    /**
     * post登录
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result login(Employee emp){
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(emp.getTelephone(),emp.getPassword());
        //进行验证，这里可以捕获异常，然后返回对应信息
        Result result = new Result();
        try {
            subject.login(usernamePasswordToken);
            result.setCode(ISystemConst.SUCCESS_CODE);
        } catch (UnknownAccountException e) {
            result.setCode(ISystemConst.DEFEAT_CODE);
        } catch (DisabledAccountException e) {
            result.setCode(ISystemConst.DISABLE_CODE);
        } catch (IncorrectCredentialsException e) {
            result.setCode(ISystemConst.PASSWORDERROR_CODE);
        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage(), e);
        }finally {
            return result;
        }
    }

}
